package com.example.smart_city_0829.base;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.smart_city_0829.SmartCity;
import com.example.smart_city_0829.retrofit.Api;
import com.example.smart_city_0829.retrofit.RetrofitBuilder;
import com.example.smart_city_0829.utils.Constant;
import com.google.gson.Gson;

public abstract class BaseFragment extends Fragment {

    protected View rootView;
    protected Api api = RetrofitBuilder.getApi();
    protected static Gson gson = new Gson();

    protected static SharedPreferences sharedPreferences;
    protected SharedPreferences.Editor editor;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = LayoutInflater.from(getActivity()).inflate(getLayoutId(), null, false);

        sharedPreferences = SmartCity.getSharedPreferences();
        editor = sharedPreferences.edit();

        initView();
        initData();
        initEvent();
        return rootView;
    }

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initEvent();

    protected void migrateTo(Class<? extends BaseActivity> clazz){
        startActivity(new Intent(getActivity(),clazz));
    }

    protected void showToastShort(String tip){
        Toast.makeText(getActivity(),tip,Toast.LENGTH_SHORT).show();
    }
}
